/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.etop.ondoc.controller;

import com.etop.ondoc.beans.Category;
import com.etop.ondoc.beans.Document;
import com.etop.ondoc.beans.User;
import com.etop.ondoc.dao.BCategoryDao;
import com.etop.ondoc.dao.BUserDao;
import com.etop.ondoc.dao.CategoryDao;
import com.etop.ondoc.dao.DaoFactory;
import com.etop.ondoc.dao.DocumentDao;
import com.etop.ondoc.dao.UserDao;
import com.etop.ondoc.util.DefaultValue;
import com.etop.ondoc.util.OndocException;
import com.etop.ondoc.util.Pager;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author jansing
 * lz_jansing@163.com
 */
@Controller
public class AdminController {
    private BCategoryDao bcd = BCategoryDao.getInstance();
    private CategoryDao cateDao = DaoFactory.getCategoryDao();
    private DocumentDao docuDao = DaoFactory.getDocumentDao();
    private UserDao userDao = DaoFactory.getUserDao();
    
    
    /**
     * 添加分类
     *
     * @return
     */
    @RequestMapping(value = "Admin_addCategory", method = RequestMethod.POST)
    public String addCategory(HttpServletRequest req) {
        int parentId;
        String cateName;
        try {
            parentId = Integer.parseInt(req.getParameter("parentId"));
            cateName = req.getParameter("cateName");
        } catch (NumberFormatException e) {
            throw new OndocException("请输入正确的信息");
        }
        Category currentCategory = bcd.load(parentId);
        if (currentCategory == null || currentCategory.getIsVaild() == Category.IS_NOT_VAILD
                || "".equals(cateName.trim())) {
            throw new OndocException("请输入正确的信息");
        }
        Category newCategory = new Category();
        newCategory.setPareCategory(currentCategory);
        newCategory.setName(cateName.trim());
        bcd.add(newCategory);
        return "redirect:/fileManagement";
    }
    
    /**
     * 更新分类
     *
     * @return
     */
    @RequestMapping(value = "Admin_updateCategory", method = RequestMethod.POST)
    public String updateCategory(HttpServletRequest req) {
        String fatherName = req.getParameter("father-name");
        String currentName = req.getParameter("current-name");
        int cateId = Integer.parseInt(req.getParameter("cateId"));
        Category cg1 = cateDao.load(cateId);
        Category cg2 = cateDao.loadByName(fatherName.trim());
        Category currentCateRoot = cateDao.load(cg1.getPareCategory().getId());
        if (!cg1.getName().equals(currentName)) {
            cg1.setName(currentName);
            cateDao.update(cg1);
        }
        if (!currentCateRoot.getName().equals(cg2.getName())) {
            //修改父分类

            cg1.setPareCategory(cg2);
            cateDao.update(cg1);
            //修改原来分类的文档数

            while (!currentCateRoot.getName().equals("root")) {
                currentCateRoot.setDocuCount(currentCateRoot.getDocuCount() - cg1.getDocuCount());
                cateDao.update(currentCateRoot);
                currentCateRoot = cateDao.load(currentCateRoot.getPareCategory().getId());
            }

            //修改修改后的分类分档数
            while (!cg2.getName().equals("root")) {
                cg2.setDocuCount(cg2.getDocuCount() + cg1.getDocuCount());
                cateDao.update(cg2);
                cg2 = cateDao.load(cg2.getPareCategory().getId());
            }
        }
        return "redirect:/fileManagement";
    }

    /**
     * 删除分类
     *
     * @return
     */
    @RequestMapping(value = "Admin_deleteCategory", method = RequestMethod.POST)
    public String deleteCategory(HttpServletRequest req) {
        int cateId = Integer.parseInt(req.getParameter("cateId"));
        Category category = cateDao.load(cateId);
        Category temp_Pcategory = cateDao.load(category.getPareCategory().getId());
        while (temp_Pcategory != null) {
            temp_Pcategory.setDocuCount(temp_Pcategory.getDocuCount() - category.getDocuCount());
            cateDao.update(temp_Pcategory);
            if (temp_Pcategory.getPareCategory() == null) {
                temp_Pcategory = null;
            } else {
                temp_Pcategory = cateDao.load(temp_Pcategory.getPareCategory().getId());
            }
        }
        cateDao.delete(category);
        return "redirect:/fileManagement";
    }

    /**
     * 分类管理页面的唯一入口
     *
     * @return
     */
    @RequestMapping(value = "fileManagement", method = RequestMethod.GET)
    public String fileManagement(HttpServletRequest req, Model model) {
        Category cate;
        try {
            cate = cateDao.load(Integer.parseInt(req.getParameter("cateId")));
        } catch (NumberFormatException e) {
            cate = cateDao.loadByName("root");
        }
        int pagesize = DefaultValue.PAGE_SIZE;
        int offset = 0;
        try {
            offset = Integer.parseInt(req.getParameter("pager.offset"));
        } catch (NumberFormatException e) {
        }
        Pager<Document> pager = docuDao.pager(offset, pagesize, cate);
        model.addAttribute("rootCategory", cateDao.loadByName("root"));
        model.addAttribute("pager", pager);
        //pager标签库需要的地址栏的条件
        model.addAttribute("cateId", cate.getId());
        return "admin/fileManagement";
    }

    /**
     * 删除文档
     *
     * @return
     */
    @RequestMapping(value = "Admin_deleteDocument", method = RequestMethod.GET)
    public String deleteDocument(HttpServletRequest req) {
        Document docu = docuDao.load(Integer.parseInt(req.getParameter("docuId")));
        docuDao.delete(docu);
        return "redirect:/fileManagement";
    }

    /**
     * 用户管理页面的唯一入口
     * @return 
     */
    @RequestMapping(value = "userManagement", method = RequestMethod.GET)
    public String userManagement(HttpServletRequest req, Model model) {
        BUserDao bud = BUserDao.getInstance();
        int offset = 0;
        try {
            offset = Integer.parseInt(req.getParameter("pager.offset"));
        } catch (Exception e) {
        }
        int pagesize = DefaultValue.PAGE_SIZE;

        Pager<User> pagers = bud.pager(offset, pagesize);
        model.addAttribute("userList", pagers);
        return "admin/userManagement";
    }

    /**
     * 删除用户
     * @return 
     */
    @RequestMapping(value = "Admindelete", method = RequestMethod.POST)
    public String delete(HttpServletRequest req) {
        BUserDao bud = BUserDao.getInstance();
        String[] ss = req.getParameterValues("deleteCheckbox");
        for (String s : ss) {
            userDao.delete(userDao.load(Integer.parseInt(s)));
        }
        return "redirect:/userManagement";
    }
}
